package com.zhang.sdk.utils.app;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 *
 * 功能：包括日期格式化、转换等方法
 * @author zhang.x
 */
@SuppressLint("SimpleDateFormat")
public class DateUtil {

    public static String timeConvert(String time, String fromFormat, String toFormat) {
        if (StringUtils.isEmpty(time)) return time;
        SimpleDateFormat from = new SimpleDateFormat(fromFormat);
        SimpleDateFormat to = new SimpleDateFormat(toFormat);
        try {
            Date date = from.parse(time);
            return date == null ? "" : to.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String timeFormat(Date time, String format) {
        SimpleDateFormat s = new SimpleDateFormat(format);
        return s.format(time);
    }

    public static String currentTimeFormat() {
        return timeFormat(new Date(), "yyyy-MM-dd HH:mm:ss");
    }

    public static String get2YDTime() {
        return timeFormat(new Date(), "yy-MM-dd");
    }

    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        String time = DateUtil.timeFormat(calendar.getTime(), "yyMMddHHmm");
        System.out.println(time);
    }
}
